<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="优惠券名字" prop="coupon_name">
				<el-input v-model="form.coupon_name" placeholder="请输入优惠券名字" clearable></el-input>
			</el-form-item>
			<el-form-item label="面值" prop="amount">
				<el-input-number v-model="form.amount" :precision="2" controls-position="right" :min="0.01"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="类型" prop="type">
				<el-select v-model="form.type" filterable style="width: 100%">
					<el-option v-for="(item, key) in types" :key="key" :label="item" :value="key" />
				</el-select>
			</el-form-item>
			<el-form-item label="领取方式" prop="receive_type">
				<el-select v-model="form.receive_type" filterable style="width: 100%">
					<el-option v-for="(item, key) in receive_types" :key="key" :label="item" :value="key" />
				</el-select>
			</el-form-item>
			<el-form-item label="适用类型" prop="use_type">
				<el-select v-model="form.use_type" filterable style="width: 100%">
					<el-option v-for="(item, key) in use_types" :key="key" :label="item" :value="key" />
				</el-select>
			</el-form-item>
			<el-form-item label="有效期(天)" prop="expired_time">
				<el-input-number v-model="form.expired_time" :precision="0" controls-position="right" :min="1"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="最低使用金额" prop="min">
				<el-input-number v-model="form.min" controls-position="right" :precision="2" :min="1"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="可领取次数" prop="limit">
				<el-input-number v-model="form.limit" controls-position="right" :precision="0" :min="1"
					style="width: 100%;"></el-input-number>
			</el-form-item>
			<!-- <el-form-item label="开始时间" prop="start_time">
				<el-date-picker style="width: 100%;" v-model="form.start_time" value-format="X" type="datetime" placeholder="选择开始时间" />
			</el-form-item>
			<el-form-item label="结束时间" prop="end_time">
				<el-date-picker style="width: 100%;" v-model="form.end_time" value-format="X" type="datetime" placeholder="选择结束时间" />
			</el-form-item> -->
			<el-form-item label="时间" prop="time">
				<el-date-picker style="width: 100%;" @change="timeChange" :default-time="TimeRange" v-model="form.time" value-format="X" type="datetimerange"
					range-separator="To" start-placeholder="开始时间" end-placeholder="结束时间" />
			</el-form-item>
			<el-form-item label="指定优惠券" prop="type_detail">
				<el-select style="width: 100%;" v-if="codes.length" v-model="form.type_detail" clearable
					placeholder="选择快递公司" filterable>
					<el-option v-for="(item, key) in codes" :key="key" :label="item.conpany_name" :value="item.code" />
				</el-select>
			</el-form-item>
			<el-form-item label="优惠券描述" prop="description">
				<el-input v-model="form.description" placeholder="请输入描述" type="textarea" clearable></el-input>
				<!-- <sc-editor v-model="form.description" placeholder="请输入描述" :templates="templates" :height="400"></sc-editor> -->
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="form.status" filterable style="width: 100%">
					<el-option v-for="(item, key) in statuss" :key="key" :label="item" :value="key" />
				</el-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		defineAsyncComponent
	} from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		computed: {
			TimeRange() {
				const [start, end] = ["00:00:00", "23:59:59"].map(time => {
				    const [hour, minute, second] = time.split(":").map(Number);
					return new Date(0, 0, 0, hour, minute, second);
				});
				return [start, end]
			}
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: "",
					coupon_name: "",
					amount: '',
					expired_time: '',
					start_time: '',
					end_time: '',
					time: ['', ''],
					min: '7',
					type: '1',
					receive_type: '1',
					use_type: '1',
					status: '1',
					description: '',
					type_detail: '',
					limit: '1'
				},
				types: {
					'1': '满减券',
					'2': '打折券'
				},
				receive_types: {
					'1': '手动',
					'2': '赠送',
					'3': '会员',
					'4': '小程序链接',
					'5': '系统发放'
				},
				use_types: {
					'1': '通用',
					'2': '指定'
				},
				statuss: {
					'1': '启用',
					'0': '禁用',
					'-1': '删除'
				},
				codes: [],
				//验证规则
				rules: {
					coupon_name: [{
						required: true,
						message: '请输入优惠券名字'
					}],
					amount: [{
						required: true,
						message: '请输入面值'
					}],
					expired_time: [{
						required: true,
						message: '请输入有效期'
					}],
					min: [{
						required: true,
						message: '请输入最低使用金额'
					}],
					start_time: [{
						required: true,
						message: '请选择开始时间',
						trigger: 'change'
					}],
					end_time: [{
						required: true,
						message: '请选择结束时间',
						trigger: 'change'
					}],
					time: [{
						required: true,
						message: '请选择时间',
						trigger: 'change'
					}],
				},
				templates: [
					{
						title: '自定义HTML模板',
						description: '',
						content: '<strong>演示模板</strong>'
					},
					{
						title: '列表',
						description: '',
						content: '<ol><li>演示列表1</li><li>演示列表2</li></ol>'
					}
				]
			}
		},
		mounted() {
			this.getCodes()
		},
		methods: {
			async getCodes() {
				var res = await this.$API.expressCompany.expressCompany.list.get({
					page: 1,
					limit: 1000,
				});
				if (res.code == 200) {
					this.codes = res.data.data
				} else {
					this.$alert(res.msg, "提示", {
						type: 'error'
					})
				}
			},
			//显示
			open(mode = 'add') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			timeChange(value) {
				this.form.start_time = value[0]
				this.form.end_time = value[1]
			},
			//表单提交方法
			async submit() {
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if (!valid) {
					return false
				}
				this.isSaveing = true;
				let form = JSON.parse(JSON.stringify(this.form))
				var res;
				if (this.form.id) {
					res = await this.$API.marketing.coupon.update.post(form);
				} else {
					res = await this.$API.marketing.coupon.create.post(form);
				}

				this.isSaveing = false;
				if (res.code == 200) {
					if (this.mode == 'add') {
						this.form.id = res.data
					}
					let form = JSON.parse(JSON.stringify(this.form))
					this.$emit('success', form, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				} else {
					this.$alert(res.message, "提示", {
						type: 'error'
					})
				}
			},
			//表单注入数据
			setData(data) {
				this.form.id = data.id
				this.form.coupon_name = data.coupon_name
				this.form.status = data.status.toString()

				this.form.type = data.type.toString()
				this.form.receive_type = data.receive_type.toString()
				this.form.use_type = data.use_type.toString()
				this.form.amount = data.amount
				this.form.expired_time = data.expired_time
				this.form.min = data.min
				this.form.time = [data.start_time, data.end_time]
				this.form.start_time = data.start_time
				this.form.end_time = data.end_time
				this.form.description = data.description
				this.form.limit = data.limit
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
